package com.szzg10.thinking.chapter18;

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

public class GZIPcompress {
	
	public static final String FILE_NAME_GZIP = "C:/Users/ZhongGe/Desktop/test.gz";

	public static void main(String[] args) throws IOException {
		//write
		File file = new File(BufferedInputFile.FILE_NAME);
		FileReader fileReader = new FileReader(file);
		BufferedReader bufferedReader = new BufferedReader(fileReader);
		
		File fileOut = new File(FILE_NAME_GZIP);
		FileOutputStream fileOutputStream = new FileOutputStream(fileOut);
		GZIPOutputStream gzipOutputStream = new GZIPOutputStream(fileOutputStream);
		BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(gzipOutputStream);
		
		String str;
		while((str = bufferedReader.readLine()) != null) {
			str += '\n';
			bufferedOutputStream.write(str.getBytes());
		}
		
		bufferedOutputStream.close();
		
		bufferedReader.close();
		fileReader.close();
		
		//read
		FileInputStream fileInputStream = new FileInputStream(fileOut);
		GZIPInputStream gzipInputStream = new GZIPInputStream(fileInputStream);
		InputStreamReader inputStreamReader = new InputStreamReader(gzipInputStream);
		bufferedReader = new BufferedReader(inputStreamReader);
		
		while((str = bufferedReader.readLine()) != null) {
			System.out.println(str);
		}
		
		bufferedReader.close();
	}
}
